<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="com.mobilottery.server.GameParticipation"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.StringTokenizer"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ include file="/pages/template/page-begin.jspf" %>
<%
	_pageTitle = "My Games"; 
	_pageId = "playgame"; 
	_mainPageId = "mygames"; 
%>
<%@ include file="/pages/template/head-begin.jspf" %>
<%@ include file="/pages/template/head-end.jspf" %>

<body>
	<%@ include file="/pages/template/content-begin.jspf" %>
	<%@ include file="/pages/template/bar-left.jspf" %>
	<%@ include file="/pages/template/bar-right.jspf" %>
	
	
<%{
		//check param
		Long playGameId = null;
		try
		{
			playGameId = Long.parseLong(request.getParameter("gameId"));
		}
		catch(Exception nfe){}
		if(playGameId==null || sessionUser==null)
		{
			response.sendRedirect("/pages/login.jsp");
			return;			
		}
		Game game = GameProvider.getInstance().getGameManager().getGame(playGameId);
		if(!"play".equals(request.getParameter("action")))
		{
%>	
	<div id="content">
		<h3>Play Game</h3>
		<hr />
			<div class="featurebox_center" style="">				
				 #<%=game.getId()%> <%=game.getDescription()%>  <br />
				 Prize: <%=ValidationForm.PRICE_FORMAT.format(game.getMaxPrize())%> <br />
				Closes: <%= ValidationForm.DATE_FORMAT.format(game.getCloseDate()) %> <br />
				<form action="/pages/playgame.jsp" method="post">
					Enter 6 numbers delimited by comma:
					<input type="hidden" name="gameId" value="<%=playGameId%>" />
					<input type="hidden" name="action" value="play" />
					<input type="text" name="guessedNumbers" value="1, 2, 3, 4, 5, 6" />
					<input type="submit" value="Play" />
				</form>  
			</div>
	
	</div>	
<%
	}
	else
	{
		boolean hasError = false;
		String message = "";
		try
		{
			List<Long> guessedNumbers = new ArrayList<Long>();
            StringTokenizer tk = new StringTokenizer(request.getParameter("guessedNumbers"), ",");
            while (tk.hasMoreTokens()) {
            	String num = tk.nextToken();
                guessedNumbers.add(Long.parseLong(num.trim()));
            }			
            if(guessedNumbers.size()!=6)
            {
            	throw new Exception("Six numbers are required");
            }
			GameParticipation participation = new GameParticipation(game, sessionUser, new Date(), guessedNumbers);
			Long transactionId = GameProvider.getInstance().addGameParticipation(participation);			
			message = "Participation was registered, reference number #" + transactionId;
		}
		catch(Exception ex)
		{
			message = "Error: " + ex.getMessage(); 
			hasError = true;
		}
%>
	<center>
	<h3 style='color:<%=hasError ? "red" : "green"%>'>
		<%=message%>
	</h3>
	<a href="/pages/mygames.jsp" >Go to My Games</a>
	</center>
	
<%		
	}
}%>
	<jsp:include page="/pages/template/content-end.jspf" />
	
</body>
</html>
